package com.rotanava.boot.system.module.system.config;
import com.google.common.collect.Lists;
import com.rotanava.boot.system.api.SysUserService;
import com.rotanava.boot.system.api.module.system.bo.SysAnnouncementConfig;
import com.rotanava.boot.system.api.module.system.bo.SysRole;
import com.rotanava.boot.system.api.module.system.bo.SysUser;
import com.rotanava.boot.system.api.module.system.dto.AddSysUserDTO;
import com.rotanava.boot.system.module.dao.SysAnnouncementConfigMapper;
import com.rotanava.boot.system.module.dao.SysRoleMapper;
import com.rotanava.boot.system.module.dao.SysUserMapper;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.Date;

/**
 * 自动生成数据
 *
 * @author weiqiangmiao
 * @date 2022/07/05
 */
@Component
@Log4j2
public class AutoGenerateData {

    @Autowired
    @Lazy
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    @Lazy
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysAnnouncementConfigMapper sysAnnouncementConfigMapper;


    @PostConstruct
    public void run() {
        SysRole sysRole = sysRoleMapper.findByRoleCode("top_admin");
        if(sysRole == null){
            final SysRole newSysRole = new SysRole();
            newSysRole.setId(1);
            newSysRole.setCreateBy(1);
            newSysRole.setCreateTime(new Date());
            newSysRole.setRoleDataScope(3);
            newSysRole.setRoleName("超级管理员");
            newSysRole.setRoleCode("top_admin");
            newSysRole.setRoleDescription("超级管理员");
            sysRoleMapper.insertAdmin(newSysRole);
        }

        SysUser sysUser = sysUserMapper.findByUserAccountName("admin");
        if(sysUser == null ){
            AddSysUserDTO addSysUserDTO = new AddSysUserDTO();
            addSysUserDTO.setUserAccountName("admin");
            addSysUserDTO.setUserName("admin");
            addSysUserDTO.setUserPassword("admin");
//            addSysUserDTO.setUserPassword("RotaNova@2020");
            addSysUserDTO.setUserSex(2);
            addSysUserDTO.setUserCode("top_admin");
            addSysUserDTO.setUserSysrole(1);
            addSysUserDTO.setResponsibleDeptList(Lists.newArrayList());
            addSysUserDTO.setUserValidTime(4099766400000L);
            addSysUserDTO.setUserStatus(1);
            addSysUserDTO.setSysRoleIdList(Lists.newArrayList(1));

            try {
                sysUserService.addSysUser(addSysUserDTO, null,1, false);
            } catch (Exception e) {
                log.error("添加admin管理员用户失败");
                e.printStackTrace();
            }
        }

        Integer count = sysAnnouncementConfigMapper.countByConfigNameIn(Lists.newArrayList("通知消息", "系统消息", "告警消息"));
        if(count != 3){
            SysAnnouncementConfig notice = new SysAnnouncementConfig();
            notice.setId(1);
            notice.setConfigName("通知消息");
            notice.setType(0);
            notice.setSysNotice(0);
            notice.setSmsNotice(0);
            notice.setWechatNotice(0);
            notice.setEmailNotice(0);
            notice.setPhoneNotice(0);
            notice.setAllowCloseNotice(0);
            notice.setAnnTarget(0);
            notice.setAnnUserIds("[]");
            notice.setAnnDeptIds("[]");
            notice.setDingTalkNotice(0);
            notice.setAnnDingTalkIds("[]");

            SysAnnouncementConfig system = new SysAnnouncementConfig();
            system.setId(2);
            system.setConfigName("系统消息");
            system.setType(1);
            system.setSysNotice(1);
            system.setSmsNotice(1);
            system.setWechatNotice(1);
            system.setEmailNotice(1);
            system.setPhoneNotice(1);
            system.setAllowCloseNotice(1);
            system.setAnnTarget(0);
            system.setAnnUserIds("[]");
            system.setAnnDeptIds("[]");
            system.setDingTalkNotice(0);
            system.setAnnDingTalkIds("[]");


            SysAnnouncementConfig alert = new SysAnnouncementConfig();
            alert.setId(3);
            alert.setConfigName("告警消息");
            alert.setType(2);
            alert.setSysNotice(0);
            alert.setSmsNotice(0);
            alert.setWechatNotice(0);
            alert.setEmailNotice(0);
            alert.setPhoneNotice(1);
            alert.setAllowCloseNotice(0);
            alert.setAnnTarget(0);
            alert.setAnnUserIds("[]");
            alert.setAnnDeptIds("[]");
            alert.setDingTalkNotice(0);
            alert.setAnnDingTalkIds("[]");

            sysAnnouncementConfigMapper.insertList(Lists.newArrayList(notice, system, alert));
        }
    }

}
